<template>
  <div class="goods-comment">
    <div class="head">
      <div class="data">
        <p>
          <span>{{ comment.salesCount }}</span
          ><span>人购买</span>
        </p>
        <p>
          <span>{{ comment.praisePercent }}</span
          ><span>好评率</span>
        </p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a
            href="javascript:;"
            v-for="(tag, index) in comment.tags"
            :key="tag.title"
            :class="{ active: tagIndex === index }"
            @click="handleTags(index)"
            >{{ tag.title }}（{{ tag.tagCount }}）</a
          >
        </div>
      </div>
    </div>
    <!-- 排序 -->
    <div class="sort">
      <span>排序：</span>
      <a
        @click="handleSort(null)"
        href="javascript:;"
        :class="{ active: reqParams.sortField === null }"
        >默认</a
      >
      <a
        @click="handleSort('praiseCount')"
        href="javascript:;"
        :class="{ active: reqParams.sortField === 'praiseCount' }"
        >最热</a
      >
      <a
        @click="handleSort('createTime')"
        href="javascript:;"
        :class="{ active: reqParams.sortField === 'createTime' }"
        >最新</a
      >
    </div>
    <!-- 列表 -->
    <div class="list">
      <div class="item" v-for="(item, index) in commentList.items" :key="index">
        <div class="user">
          <img v-src="item.member.avatar" alt="" />
          <span>{{ formatNickname(item.member.nickname) }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i class="iconfont icon-wjx01"></i>
            <i class="iconfont icon-wjx01"></i>
            <i class="iconfont icon-wjx01"></i>
            <i class="iconfont icon-wjx01"></i>
            <i class="iconfont icon-wjx02"></i>
            <span class="attr">{{ formatSpecs(item.orderInfo.specs) }}</span>
          </div>
          <div class="text">
            {{ item.content }}
          </div>
          <GoodsCommentImage
            v-if="item.pictures.length"
            :pictures="item.pictures"
          />
          <div class="time">
            <span>{{ item.createTime }}</span>
            <span class="zan">
              <i class="iconfont icon-dianzan"> </i>{{ item.praiseCount }}
            </span>
          </div>
        </div>
      </div>
      <XtxPagination
        :total="total"
        :btnCount="5"
        v-model:currentPage="reqParams.page"
      ></XtxPagination>
    </div>
  </div>
</template>

<script>
import { findGoodsCommentInfo, findGoodsCommentListAPI } from '@/api/goods'
import { computed, onBeforeMount, reactive, ref, watch } from 'vue'
import GoodsCommentImage from './goods-comment-image.vue'
export default {
  components: { GoodsCommentImage },
  name: 'GoodsComment',
  setup () {
    const comment = ref({}) // 评论数据
    const tagIndex = ref(0) // tags
    const commentList = ref({}) // 评论列表

    // 切换 tags
    const handleTags = index => {
      tagIndex.value = index
      // 设置有图和标签条件
      const currTag = comment.value.tags[index]
      if (currTag.type === 'all') {
        // 全部类型
        reqParams.hasPicture = false
        reqParams.tag = null
      } else if (currTag.type === 'img') {
        // 有图类型
        reqParams.hasPicture = true
        reqParams.tag = null
      } else {
        // 其他类型
        reqParams.hasPicture = false
        reqParams.tag = currTag.title
      }
      reqParams.page = 1 // 切换 tag 的时候重置页码
    }

    // 定义转换数据的函数（对应vue2.0的过滤器）
    const formatSpecs = specs =>
      specs.reduce((p, c) => `${p} ${c.name}：${c.nameValue}`, '').trim()

    const formatNickname = nickname =>
      nickname.substr(0, 1) + '****' + nickname.substr(-1)

    // 筛选条件准备
    const reqParams = reactive({
      page: 1,
      pageSize: 10,
      hasPicture: null,
      tag: null,
      sortField: null
    })

    // 分页数据准备
    const total = computed(() => commentList.value.counts || 0)

    // 获取评论数据
    onBeforeMount(async () => {
      const res = await findGoodsCommentInfo()
      comment.value = res
      comment.value.tags.unshift({
        type: 'img',
        title: '有图',
        tagCount: res.hasPictureCount
      })
      comment.value.tags.unshift({
        type: 'all',
        title: '全部评价',
        tagCount: res.evaluateCount
      })
    })

    // 获取评论列表
    watch(
      reqParams,
      async () => {
        commentList.value = await findGoodsCommentListAPI(reqParams)
      },
      { deep: true, immediate: true }
    )

    // 改变排序规则
    const handleSort = type => {
      reqParams.sortField = type
      reqParams.page = 1
    }

    return {
      comment,
      tagIndex,
      reqParams,
      handleSort,
      handleTags,
      commentList,
      formatSpecs,
      formatNickname,
      total
    }
  }
}
</script>

<style scoped lang="less">
.list {
  padding: 0 20px;
  .item {
    display: flex;
    padding: 25px 10px;
    border-bottom: 1px solid #f5f5f5;
    .user {
      width: 160px;
      img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        overflow: hidden;
      }
      span {
        padding-left: 10px;
        color: #666;
      }
    }
    .body {
      flex: 1;
      .score {
        line-height: 40px;
        .iconfont {
          color: #ff9240;
          padding-right: 3px;
        }
        .attr {
          padding-left: 10px;
          color: #666;
        }
      }
    }
    .text {
      color: #666;
      line-height: 24px;
    }
    .time {
      color: #999;
      display: flex;
      justify-content: space-between;
      margin-top: 5px;
    }
  }
}
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }
          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
}
</style>
